#!/usr/bin/perl -w

# Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
# Copyright (C) 2006-2010 RobotCub Consortium
# All rights reserved.
#
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license. See the accompanying LICENSE file for details.

# use Data::Dumper;

my $fname = $ARGV[0];

# print "$fname\n";


my $txt = "";
open(FIN,"<license_check/$fname");
while (<FIN>) {
    $txt .= $_;
}
close(FIN);



### Handle Policy
my $policy = "unknown";

if ($txt =~ /CopyPolicy\:\s*(.*)/i) {
    $policy = $1;
    if ($policy eq "Released under the terms of the LGPLv2.1 or later, see LICENSE") {
        $policy = "LGPLv2.1 or later";
    }
    if ($policy eq "Released under the terms of the GNU GPL v2.0.") {
        $policy = "GPLv2";
    }
} elsif ($txt =~ /BSD-3-Clause/) {
    $policy = "BSD-3-Clause";
} elsif ($txt =~ /clause BSD/) {
    if ($txt =~ /2.clause BSD/) {
        $policy = "BSD-2-Clause";
    } elsif ($txt =~ /3.clause BSD/) {
        $policy = "BSD-3-Clause";
    } elsif ($txt =~ /4.clause BSD/) {
        $policy = "BSD-4-Clause";
    }
} elsif ($txt =~ /Distributed under the OSI-approved BSD License/) {
    $policy = "BSD-3-Clause";
} elsif ($txt =~ /Redistribution and use is allowed according to the terms of the BSD license/) {
    $policy = "BSD-3-Clause";
} elsif ($txt =~ /Permission is granted to anyone to use this software for any/) {
    if ($txt =~ /This notice may not be removed or altered from any source/) {
        $policy = "zlib";
    } else {
        if ($txt =~ /All advertising materials mentioning features or use of this software/) {
            $policy = "BSD-4-Clause";
        } else {
            $policy = "BSD-3-Clause";
        }
    }
} elsif ($txt =~ /Redistribution and use in source and binary forms, with or without/) {
    if ($txt =~ /All advertising materials mentioning features or use of this software/) {
        $policy = "BSD-4-Clause";
    } elsif ($txt =~ /may be used to endorse or promote/) {
        $policy = "BSD-3-Clause";
    } else {
        $policy = "BSD-2-Clause";
    }
} elsif ($txt =~ /GNU Lesser General Public/) {
    if ($txt =~ /version 2.1.+later/) {
        $policy = "LGPLv2.1 or later";
    }
} elsif ($txt =~ /LGPLv2.1\+/) {
    $policy = "LGPLv2.1 or later";
} elsif ($txt =~ /GNU General Public License/) {
    if ($txt =~ /version 2 of the License, or/) {
        $policy = "GPLv2 or later";
    } elsif ($txt =~ /version 3 of the License, or/) {
        $policy = "GPLv3 or later";
    }
} elsif ($txt =~ /GNU Library General Public/) {
    if ($txt =~ /version 2 of the License. or/) {
        $policy = "LGPLv2 or later";
    }
} elsif ($txt =~ /GNU Lesser General Public/) {
    if ($txt =~ /version 2 of the License. or/) {
        $policy = "LGPLv2 or later";
    }
} elsif ($txt =~ /Licensed to the Apache Software Foundation/) {
    $policy = "Apache License, Version 2.0";
} elsif ($txt =~ /Licensed under the Apache License/) {
    $policy = "Apache License, Version 2.0";
} elsif ($fname =~ /sfun_time.c/) {
    $policy = "BSD-3-Clause";
}

$policy =~ s/\s+$//g;
$policy =~ s/^\s+//g;

# print "$policy\n";



### Handle Copyright

my @copyright = "unknown";
# Match lines in files
if ($txt =~ /copyright\:\s*([^ \"\'\*\r\n]+)\.?\s*[\n\r\*]/i) {
    @copyright = ($txt =~ /copyright\:\s*([^ \"\'\*\r\n]+)\.?\s*[\n\r\*]/ig);
} elsif ($txt =~ /copyright\:?\s*([^\(0-9][^\"\'\*\r\n]+)\.?\s*[\n\r\*]/i) {
    @copyright = ($txt =~ /copyright\:?\s*([^\(0-9][^\"\'\*\r\n]+)\.?\s*[\n\r\*]/ig);
} elsif ($fname =~ /sfun_time.c/) {
    @copyright = "2009 The MathWorks, Inc.";
} elsif ($txt =~ /Licensed to the Apache Software Foundation/) {
    @copyright = "Apache Software Foundation";
}

# Cleanup extracted strings
for(@copyright) {s/^\s+//g};
for(@copyright) {s/\s+$//g};
for(@copyright) {s/\(c\)/\(C\)/g};
for(@copyright) {s/©/\(C\)/g};
for(@copyright) {s/\xa9/\(C\)/g};
for(@copyright) {s/\.$//g};
for(@copyright) {s/, Inc$/, Inc./g};
for(@copyright) {s/^\s*:\s*//g};

# Remove lines that match specific patterns that are not copyrights.
@copyright = grep(!/QString/, @copyright);
@copyright = grep(!/setText/, @copyright);
@copyright = grep(!/vSpacing/, @copyright);
@copyright = grep(!/0\)\)\)/, @copyright);
@copyright = grep(!/HOLDER/, @copyright);
@copyright = grep(!/holder/, @copyright);
@copyright = grep(!/into macOS bundles/, @copyright);
@copyright = grep(!/statement follows/, @copyright);
@copyright = grep(!/this permission notice/, @copyright);
@copyright = grep(!/\\n\\/, @copyright);
@copyright = grep(!/NO_EDITOR/, @copyright);

# If we removed all lines reset to "unknown"
if (scalar(@copyright) == 0) {
    @copyright = "unknown";
    $policy = "unknown"
}

# print Dumper(@copyright);

### Write results

my $txt2 = "$fname\n  Copyright: " . join("\n  Copyright: ", @copyright) . "\n  CopyPolicy: $policy\n\n";
open(FOUT,">>license-changes.txt");
print FOUT $txt2;
close(FOUT);
if ($policy =~ /BSD-3-Clause/) {
    open(FOUT,">>license-bsd3.txt");
    print FOUT $txt2;
    close(FOUT);
} elsif ($policy =~ /BSD-2-Clause/) {
    open(FOUT,">>license-bsd2.txt");
    print FOUT $txt2;
    close(FOUT);
} elsif ($policy =~ /BSD-4-Clause/) {
    open(FOUT,">>license-bsd4.txt");
    print FOUT $txt2;
    close(FOUT);
} elsif ($policy =~ /LGPLv2.1 or later/) {
    open(FOUT,">>license-lgpl2.1+.txt");
    print FOUT $txt2;
    close(FOUT);
} elsif ($policy =~ /GPLv2 or later/) {
    open(FOUT,">>license-gpl2+.txt");
    print FOUT $txt2;
    close(FOUT);
} elsif ($policy =~ /GPLv3 or later/) {
    open(FOUT,">>license-gpl3+.txt");
    print FOUT $txt2;
    close(FOUT);
} else {
    open(FOUT,">>license-odd.txt");
    print FOUT $txt2;
    close(FOUT);
}

# print "$txt2\n";
